﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class QuarterPlan_QuarterPlanSlaveDepartment : System.Web.UI.Page
{
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    QuarterPlanofManager dllQuarterPlan = new QuarterPlanofManager();
    QuarterPlanDetailManager dllQuarterPlanDetail = new QuarterPlanDetailManager();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {

            #region 弹出提醒
            PopupManager dllPopupManager = new PopupManager();
            string _info = dllPopupManager.getPopupInfo();
            if (_info.Length > 0)
            {
                ShowMessage(_info);
            }
            #endregion

            bindGridView();
        }
    }


    /// <summary>
    /// 弹出小窗体事件
    /// </summary>
    /// <param name="msg"></param>
    public void ShowMessage(string msg)
    {
        EeekSoft.Web.PopupWin pop = new EeekSoft.Web.PopupWin();
        this.Controls.Add(pop);
        pop.Text = msg;
        pop.Message = msg;
        pop.Title = System.Configuration.ConfigurationManager.AppSettings["CompanyName"].ToString();
        pop.WindowScroll = true;
        pop.HideAfter = 99999999;
        pop.DockMode = EeekSoft.Web.PopupDocking.BottomLeft;
        pop.OffsetX = int.Parse(Session["_width"].ToString()) - 338;
        pop.ActionType = EeekSoft.Web.PopupAction.MessageWindow;
        pop.OffsetY = 1;
        pop.Width = 220;
        pop.Height = 170;
    }



    /// <summary>
    /// 获得登录用户真实姓名
    /// </summary>
    /// <returns></returns>
    public string getRealName()
    {
        string _realName = "";
        Model.UserInfo objUserInfo = new Model.UserInfo();
        object[] _value = new object[2];
        _value[0] = objUserInfo.UserName;
        _value[1] = objUserInfo.UserName;
        System.Data.DataTable _userInfo = dllUserInfoManager.get(BLL.UserInfoManager.field.UserName.ToString(), _value);
        if (_userInfo.Rows.Count > 0)
        {
            _realName = _userInfo.Rows[0]["RealName"].ToString();
        }
        return _realName;
    }


    //构建上年计划表结构并添加1行
    DataTable createDataTable()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("QuarterPlanSortId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("WorkItem");
        _dt.Columns.Add(dc);
        dc = new DataColumn("StartTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CompleteTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MasterPeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartment");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartmentDuty");
        _dt.Columns.Add(dc);
        dc = new DataColumn("EvaluatePeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PlanSendState");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CheckNote");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Remart");
        _dt.Columns.Add(dc);
        dc = new DataColumn("QuarterPlanId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("AttachmentPath");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["QuarterPlanSortId"] = "";
        dr["WorkItem"] = "";
        dr["CompleteTime"] = "";
        dr["StartTime"] = "";
        dr["MasterPeople"] = "";
        dr["SlaveDepartment"] = "";
        dr["SlaveDepartmentDuty"] = "";
        dr["EvaluatePeople"] = "";
        dr["PlanSendState"] = "0";
        dr["CheckNote"] = "";
        dr["Remart"] = "";
        dr["QuarterPlanId"] = "";
        dr["AttachmentPath"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }


    /// <summary>
    /// 绑定gridview事件
    /// </summary>
    void bindGridView()
    {
        //得到用户名
        this.gvlist.DataSource = new DataTable();
        this.gvlist.DataBind();
        Model.UserInfo _objUserInfo = new Model.UserInfo();
        object[] _value = new object[2];
        _value[0] = getRealName();
        _value[1] = _objUserInfo.UserName;
        DataSet ds = dllQuarterPlan.getQuarterSlaveDepartment(_value);
        DataTable _quarterPlan = new DataTable();
        if (ds.Tables.Count > 1)
        {
            _quarterPlan = dllQuarterPlan.getQuarterSlaveDepartment(_value).Tables[1];
        }
        else
        {
            _quarterPlan = dllQuarterPlan.getQuarterSlaveDepartment(_value).Tables[0];
        }

        DataView dv = new DataView(_quarterPlan);
        dv.Sort = "QuarterPlanSortId";
        this.gvlist.DataSource = dv;
        this.gvlist.DataBind();

        if (this.gvlist.Rows.Count == 0)
        {
            this.gvlist.DataSource = createDataTable();
            this.gvlist.DataBind();
        }
        #region    截取字符串处理，是否显示查看年分解按钮
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {

            object[] _valueQuarterPlanResolve = new object[1];
            _valueQuarterPlanResolve[0] = this.gvlist.DataKeys[i]["QuarterPlanId"].ToString();
            DataTable _quarterResolve = dllQuarterPlanDetail.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _valueQuarterPlanResolve);
            if (_quarterResolve.Rows.Count > 0)
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = true;
                lbl.Visible = false;
            }
            else
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = false;
                lbl.Visible = true;
            }
            wrapByComma(this.gvlist, i, 6);
            wrapByComma(this.gvlist, i, 7);
            if (this.gvlist.Rows[i].Cells[10].Text != "&nbsp;" && this.gvlist.Rows[i].Cells[10].Text != "")
            {
                if (Convert.ToInt32(this.gvlist.Rows[i].Cells[10].Text) == 4)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.FromName("#00C0C0");
                }
                if (Convert.ToInt32(this.gvlist.Rows[i].Cells[10].Text) == 5)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.FromName("#00C000");
                }
                if (Convert.ToInt32(this.gvlist.Rows[i].Cells[10].Text) == 6)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Yellow;
                }
                if (Convert.ToInt32(this.gvlist.Rows[i].Cells[10].Text) == 7)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Red;
                }
                if (Convert.ToInt32(this.gvlist.Rows[i].Cells[10].Text) > 2)
                {
                    this.gvlist.Rows[i].Cells[10].Text = "达成共识";
                }
                else
                {
                    this.gvlist.Rows[i].Cells[10].Text = "";
                }
            }
            if (this.gvlist.Rows[i].Cells[11].Text == "&nbsp;" || this.gvlist.Rows[i].Cells[11].Text == "&amp;nbsp;")
            {
                this.gvlist.Rows[i].Cells[11].Text = "";
            }


        }
        #endregion
        #region 下载文件
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {

            if (this.gvlist.DataKeys[i]["AttachmentPath"].ToString().Length == 0)
            {
                Button _btn = (Button)this.gvlist.Rows[i].Cells[13].FindControl("btnDownLoad");
                _btn.Visible = false;
                Label _lbl = (Label)this.gvlist.Rows[i].Cells[13].FindControl("lblAlert");
                _lbl.Visible = true;
                _lbl.Text = "无附件";
            }

        }
        #endregion
    }


    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }



    /// <summary>
    /// 下载
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDownLoad_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button btt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)btt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        //读取隐藏地址列
        string path = this.gvlist.DataKeys[i]["AttachmentPath"].ToString();
        // Response.Redirect("~/upload/"+path);
        FileDownload(path);
        // Utility.HttpHelp.DownloadFile(path);
    }

    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }

    /// <summary>
    /// 查看年分解计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuarterPlanResolve_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引        
        int _quarterPlanId = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
        string _url = "QuarterPlanResolveDetail.aspx?_quarterPlanId=" + _quarterPlanId;
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=350,width=900,top=200,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }

}
